package com.lanou.musicproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.AlbumContentACtivity;
import com.lanou.musicproject.bean.Album;
import com.lanou.musicproject.bean.AlbumContent;
import com.lanou.musicproject.utils.MyConfig;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class AlbumListViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<Album.DataEntity>data;
    private Album.DataEntity entity;
    public AlbumListViewAdapter(Context c, List<Album.DataEntity>d){
        mContext = c;
        data = d;
    }
    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHodler hodler = null ;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.album_item,null);
            hodler = new ViewHodler();
            hodler.ablum_Pic = (ImageView) convertView.findViewById(R.id.ablum_Pic);
            hodler.album_title = (TextView) convertView.findViewById(R.id.album_title);
            hodler.album_publishDate = (TextView) convertView.findViewById(R.id.album_publishDate);
            hodler.album_singer = (TextView) convertView.findViewById(R.id.album_singer);
            hodler.ablum_line = (LinearLayout) convertView.findViewById(R.id.ablum_line);
            convertView.setTag(hodler);
        }else {
            hodler = (ViewHodler) convertView.getTag();
        }
        entity = (Album.DataEntity) getItem(position);
        if (entity != null){
            hodler.album_title.setText(entity.getName());
            hodler.album_singer.setText(entity.getSingerName());
            hodler.album_publishDate.setText(entity.getPublishDate());
            final String url = entity.getPicUrl();
            Picasso.with(mContext).load(url).into(hodler.ablum_Pic);
            hodler.ablum_line.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   String album = data.get(position).getAlbumId()+"";
                    Intent intent =new Intent(mContext, AlbumContentACtivity.class);
                    String image = data.get(position).getPicUrl();
                    intent.putExtra("url", image);
                    MyConfig.putSharePre(mContext, "album", "albumid", album);
                    mContext.startActivity(intent);

                }
            });
        }

        return convertView;
    }
  private class  ViewHodler {
      private ImageView ablum_Pic;
      private TextView album_title,album_singer,album_publishDate;
      private LinearLayout ablum_line;

  }
}
